package cn.iocoder.yudao.module.wms.service.driver;

import cn.hutool.core.date.LocalDateTimeUtil;
import cn.iocoder.yudao.module.wms.controller.admin.employee.vo.EmployeeSaveReqVO;
import cn.iocoder.yudao.module.wms.dal.dataobject.employee.EmployeeDO;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.*;
import cn.iocoder.yudao.module.wms.controller.admin.driver.vo.*;
import cn.iocoder.yudao.module.wms.dal.dataobject.driver.DriverDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.wms.dal.mysql.driver.DriverMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.wms.enums.ErrorCodeConstants.*;

/**
 * 驾驶员资料 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class DriverServiceImpl extends ServiceImpl<DriverMapper, DriverDO> implements DriverService {

    @Resource
    private DriverMapper driverMapper;

    @Override
    public String createDriver(DriverSaveReqVO reqVO) {
        // 通过联系电话或身份证查询驾驶员是否存在
        boolean exists = this.exists(Wrappers.lambdaQuery(DriverDO.class)
                .eq(DriverDO::getPhone, reqVO.getPhone().trim())
                .or()
                .eq(DriverDO::getIdCard, reqVO.getIdCard().trim())
        );
        if (exists) {
            throw exception(EMPLOYEE_PHONE_OR_ID_CARD_EXISTS);
        }
        // 插入
        DriverDO driverDO = BeanUtils.toBean(reqVO, DriverDO.class);
        String now = LocalDateTimeUtil.formatNormal(LocalDateTime.now());
        driverDO.setId(UUID.randomUUID().toString())
                .setDriving(false)
                .setCreateAt(now)
                .setUpdateAt(now);
        driverMapper.insert(driverDO);
        // 返回
        return driverDO.getId();
    }

    @Override
    public void updateDriver(DriverSaveReqVO reqVO) {
        // 校验存在
        validateDriverExists(reqVO.getId());
        // 通过联系电话或身份证查询驾驶员是否存在
        boolean exists = this.exists(Wrappers.lambdaQuery(DriverDO.class)
                .ne(DriverDO::getId, reqVO.getId())
                .and(o-> o.eq(DriverDO::getPhone, reqVO.getPhone().trim())
                        .or()
                        .eq(DriverDO::getIdCard, reqVO.getIdCard().trim()))
        );
        if (exists) {
            throw exception(EMPLOYEE_PHONE_OR_ID_CARD_EXISTS);
        }
        // 更新
        DriverDO updateObj = BeanUtils.toBean(reqVO, DriverDO.class);
        updateObj.setUpdateAt(LocalDateTimeUtil.formatNormal(LocalDateTime.now()));
        driverMapper.updateById(updateObj);
    }

    @Override
    public void deleteDriver(String id) {
        // 校验存在
        validateDriverExists(id);
        // 删除
        driverMapper.deleteById(id);
    }

    private void validateDriverExists(String id) {
        if (driverMapper.selectById(id) == null) {
            throw exception(DRIVER_NOT_EXISTS);
        }
    }

    @Override
    public DriverDO getDriver(String id) {
        return driverMapper.selectById(id);
    }

    @Override
    public PageResult<DriverDO> getDriverPage(DriverPageReqVO pageReqVO) {
        return driverMapper.selectPage(pageReqVO);
    }

}